#include "DarkWidget.h"
#include <QHBoxLayout>
#include <QEvent>

DarkWidget::DarkWidget(QWidget*p)
    :QWidget(p)
{
    setMinimumSize(200, 200);
    setWindowFlags(Qt::FramelessWindowHint);
    setFocusPolicy(Qt::FocusPolicy::StrongFocus);
    m_pIconLabel = new QLabel(this);
    m_pTitleLabel = new QLabel(this);
    m_pMinimizeButton = new QPushButton(this);
    m_pMaximizeButton = new QPushButton(this);
    m_pCloseButton = new QPushButton(this);

    m_pMinimizeButton->installEventFilter(this);
    m_pMaximizeButton->installEventFilter(this);
    m_pCloseButton->installEventFilter(this);
    //this->installEventFilter(this);


    m_pIconLabel->setFixedSize(1, 1);
    m_pIconLabel->setScaledContents(true);

    m_pTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    m_pMinimizeButton->setFixedSize(20, 20);
    m_pMaximizeButton->setFixedSize(20, 20);
    m_pCloseButton->setFixedSize(20, 20);

    m_pMinimizeButton->setIcon(QIcon(":/images/icon_window_minimize.png"));
    m_pMaximizeButton->setIcon(QIcon(":/images/icon_window_maximize.png"));
    m_pCloseButton->setIcon(QIcon(":/images/icon_window_close.png"));

    m_pMinimizeButton->setFlat(true);
    m_pMinimizeButton->setStyleSheet("QPushButton{background: transparent;}");
    m_pMaximizeButton->setFlat(true);
    m_pMaximizeButton->setStyleSheet("QPushButton{background: transparent;}");
    m_pCloseButton->setFlat(true);
    m_pCloseButton->setStyleSheet("QPushButton{background: transparent;}");

    QHBoxLayout *pLayout = new QHBoxLayout(this);
    pLayout->addWidget(m_pIconLabel);
    pLayout->addSpacing(5);
    pLayout->addWidget(m_pTitleLabel);
    pLayout->addWidget(m_pMinimizeButton);
    pLayout->addWidget(m_pMaximizeButton);
    pLayout->addWidget(m_pCloseButton);
    pLayout->setSpacing(5);
    pLayout->setContentsMargins(5, 0, 5, 0);

    QWidget* titleWidget = new QWidget();
    titleWidget->setFixedHeight(20);
    titleWidget->setLayout(pLayout);

    QPalette pal(this->palette());
    pal.setColor(QPalette::Background, QColor(53, 53, 53));
    this->setAutoFillBackground(true);
    this->setPalette(pal);

    QVBoxLayout*  verticalLayout = new QVBoxLayout();
    verticalLayout = new QVBoxLayout(this);
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    verticalLayout->addWidget(titleWidget);

    contentLayeout = new QVBoxLayout();
    contentLayeout->setContentsMargins(0, 0, 0, 0);
    verticalLayout->addLayout(contentLayeout);

    setLayout(verticalLayout);

    connect(m_pMinimizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
    connect(m_pMaximizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
    connect(m_pCloseButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
    m_pMinimizeButton->setVisible(false);
}


DarkWidget::~DarkWidget()
{
}

void DarkWidget::setContentWidget(QWidget * w)
{
    setWindowTitle(w->windowTitle());
    contentLayeout->addWidget(w);
    this->update();
    m_widget = w;
}

QWidget * DarkWidget::getContentWidget()
{
    return m_widget;
}

bool DarkWidget::eventFilter(QObject *obj, QEvent *event)
{
    switch (event->type())
    {
    case QEvent::WindowTitleChange:
    {
        QWidget *pWidget = qobject_cast<QWidget *>(obj);
        if (pWidget)
        {
            m_pTitleLabel->setText(pWidget->windowTitle());
            return true;
        }
    }
    case QEvent::WindowIconChange:
    {
        QWidget *pWidget = qobject_cast<QWidget *>(obj);
        if (pWidget)
        {
            QIcon icon = pWidget->windowIcon();
            m_pIconLabel->setPixmap(icon.pixmap(m_pIconLabel->size()));
            return true;
        }
    }
    case QEvent::WindowStateChange:
    case QEvent::Resize:
        updateMaximize();
        return true;
    }
    return QWidget::eventFilter(obj, event);
}

void DarkWidget::updateMaximize()
{
}

bool DarkWidget::event(QEvent * ev)
{
    if (ev->type() == QEvent::WindowStateChange 
        || ev->type() == QEvent::Resize) {
        if (this->isMaximized()) {
            m_pMaximizeButton->setVisible(false);
        }
        else {
            m_pMaximizeButton->setVisible(true);
        }
        return true;
    }
    // Make sure the rest of events are handled
    return QWidget::event(ev);
}



void DarkWidget::onClicked()
{
    QPushButton *pButton = qobject_cast<QPushButton *>(sender());
    if (pButton == m_pMinimizeButton)
    {
        this->showMinimized();
    }
    else if (pButton == m_pMaximizeButton)
    {
        this->showMaximized();
    }
    else if (pButton == m_pCloseButton)
    {
        this->close();
        this->deleteLater();
    }
}